package com.sander.guardmanagement.ui.activity;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.EditText;
import android.widget.FrameLayout;
import android.widget.ListView;
import android.widget.TextView;

import com.sander.guardmanagement.R;
import com.sander.guardmanagement.db.CityDB;
import com.sander.guardmanagement.http.BaseCallback;
import com.sander.guardmanagement.http.HomeGetCommunity;
import com.sander.guardmanagement.http.HomeGetCommunityCity;
import com.sander.guardmanagement.http.OkHttpHelper;
import com.sander.guardmanagement.model.City;
import com.sander.guardmanagement.model.District;
import com.sander.guardmanagement.response.ResponseHomeGetCityData;
import com.sander.guardmanagement.response.ResponseHomeGetCommunity;
import com.sander.guardmanagement.response.ResponseHomeGetCommunityData;
import com.sander.guardmanagement.response.ResponseHomegetHotCitysData;
import com.sander.guardmanagement.ui.view.CityListAdapter;
import com.sander.guardmanagement.ui.view.DistrictListAdapter;
import com.squareup.okhttp.Request;
import com.squareup.okhttp.Response;

import java.util.ArrayList;
import java.util.List;

public class DistrictListActivity extends AppCompatActivity {

    public ResponseHomegetHotCitysData city ;

    private List<ResponseHomeGetCommunityData> districtList = new ArrayList<>();

    public  DistrictListAdapter cityListAdapter;

    EditText searchBar ;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_district_list);
        Intent intent = this.getIntent();
        city=(ResponseHomegetHotCitysData) intent.getSerializableExtra("city");
        initCallBack();
        initData();
        initView();
    }
    private void initCallBack(){
        FrameLayout frameLayout = (FrameLayout) findViewById(R.id.callbackLayout);
        frameLayout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
    }

    private void initData(){
        if (city.getCity_id() == -1){
            HomeGetCommunityCity homeGetCommunityCity = new HomeGetCommunityCity();
            String city_name = "";
            try {
                city_name = new String(city.getCity_name().getBytes(), "UTF-8");
            }catch (Exception e){
                e.printStackTrace();
            }

            homeGetCommunityCity.city_name = city_name;
            OkHttpHelper.getinstance().post(homeGetCommunityCity.requestUrl(), homeGetCommunityCity.getValueOfClas(), new BaseCallback<ResponseHomeGetCommunity>() {
                @Override
                public void onRequestBefore() {

                }

                @Override
                public void onFailure(Request request, Exception e) {

                }

                @Override
                public void onSuccess(Response response, ResponseHomeGetCommunity o) {
                    Log.d("sander",o.getMsg());
                    if (o.getCode() == 0){
                        List<ResponseHomeGetCommunityData> list = o.getData();
                        districtList = list;
                        updateListData();
                    }
                }

                @Override
                public void onError(Response response, int errorCode, Exception e) {

                }
            });
        }else{
            HomeGetCommunity homeGetCommunity = new HomeGetCommunity();
            homeGetCommunity.city_id = city.getCity_id() + "";
            OkHttpHelper.getinstance().post(homeGetCommunity.requestUrl(), homeGetCommunity.getValueOfClas(), new BaseCallback<ResponseHomeGetCommunity>() {
                @Override
                public void onRequestBefore() {

                }

                @Override
                public void onFailure(Request request, Exception e) {

                }

                @Override
                public void onSuccess(Response response, ResponseHomeGetCommunity o) {
                    Log.d("sander",o.getMsg());
                    if (o.getCode() == 0){
                        List<ResponseHomeGetCommunityData> list = o.getData();
                        districtList = list;
                        updateListData();
                    }
                }

                @Override
                public void onError(Response response, int errorCode, Exception e) {

                }
            });
        }





//        for (int  i  = 0; i < 10; i ++){
//            District district = new District();
//            this.districtList.add(district);
//        }


    }

    private void initView(){


//        searchBar = (EditText) findViewById(R.id.search_textbarview);
        TextView textView = (TextView) findViewById(R.id.toolbarTitle);
        textView.setText(city.getCity_name());

        ListView listView = (ListView) findViewById(R.id.districtListView);

        cityListAdapter = new DistrictListAdapter(this,this.districtList);
        listView.setAdapter(cityListAdapter);
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                ResponseHomeGetCommunityData district = districtList.get(position);
                onClickItemDistrict(district);
            }
        });



    }

    public void onClickItemDistrict(ResponseHomeGetCommunityData district){
        Intent intent = new Intent(DistrictListActivity.this,DistrictSelectedUnitActivity.class);

        Bundle bundle = new Bundle();
//        bundle.putString("district",district.getDistrict());

        bundle.putSerializable("district",district);
        intent.putExtras(bundle);
        startActivity(intent);


    }

    public void updateListData(){
        if (cityListAdapter != null){
            cityListAdapter.list = districtList;
            cityListAdapter.notifyDataSetChanged();
        }

    }
}
